package pt.utl.ist.project_ccu;

import java.util.HashMap;

import pt.utl.ist.project_ccu.android_libs.Photo;
import pt.utl.ist.project_ccu.android_libs.SavedData;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;

public class PhotosAddScreen extends Activity {
	
	//variable for selection intent
	private final int PICKER = 1;
	
	HashMap<String,Button> buttons;
	Button buttonmobile;
	Button buttonwifi;
	Button buttonusb;
	Button buttonnetworks;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_photos_add_screen);
        
        setupButtons();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_photos_add_screen, menu);
        return true;
    }
    
    @Override
    public void onResume() {
    	super.onResume();
    	SavedData.getInstance().updateActivity(this);
    }
    
    public void setupButtons(){
    	buttons = new HashMap<String,Button>();
    	
    	buttonmobile = (Button) findViewById(R.id.buttonmobile);
    	buttonwifi = (Button) findViewById(R.id.buttonwifi);
    	buttonusb = (Button) findViewById(R.id.buttonusb);
    	buttonnetworks = (Button) findViewById(R.id.buttonnetworks);

    	buttons.put("buttonmobile", buttonmobile);
    	buttons.put("buttonwifi", buttonwifi);
    	buttons.put("buttonusb", buttonusb);
    	buttons.put("buttonnetworks", buttonnetworks);

    	buttonmobile.setOnClickListener(new OnClickListener() {

    		public void onClick(View arg0) {
    			/*Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
    			photoPickerIntent.setType("image/*");
    			startActivityForResult(Intent.createChooser(photoPickerIntent, "Select Photos"), PICKER);*/
    			startActivity(new Intent (getApplicationContext(), MobileGallery.class));
    		}
    	}); 

    	buttonnetworks.setOnClickListener(new OnClickListener() {

    		public void onClick(View arg0) {
    			startActivity(new Intent (getApplicationContext(), SocialNetwork.class));
    		}
    	}); 
    	
    	buttonwifi.setOnClickListener(new OnClickListener() {

    		public void onClick(View arg0) {
    			startActivity(new Intent (getApplicationContext(), WiFiLoading.class));
    		}
    	});
    	
    	buttonusb.setOnClickListener(new OnClickListener() {

    		public void onClick(View arg0) {
    			startActivity(new Intent (getApplicationContext(), WiFiLoading.class));
    		}
    	});
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    { 
    	super.onActivityResult(requestCode, resultCode, data);
    	if (resultCode == RESULT_OK) {
    		//check if we are returning from picture selection
    		if (requestCode == PICKER) {
    			
    			//Uri chosenImageUri = data.getData();
    			//String picturePath = chosenImageUri.getPath();
    			
    			try {
    			Uri chosenImageUri = data.getData();

    			String[] filePathColumn = { MediaStore.Images.Media.DATA };
    			 
                Cursor cursor = getContentResolver().query(chosenImageUri,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();
     
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();
				
                Log.i("AQUI",picturePath);
                
    			Photo photo = new Photo(picturePath);

    			SavedData.getInstance().addPhoto(photo);

    			startActivity(new Intent (getApplicationContext(), AddTags.class));
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		} 
    	}
    	
    }

}
